
package com.vehicle.controller;

import com.vehicle.bean.DataGridView;
import com.vehicle.bean.ResultDto;
import com.vehicle.pojo.Dept;
import com.vehicle.pojo.Driver;
import com.vehicle.pojo.License;
import com.vehicle.service.DriverService;
import com.vehicle.service.LicenseService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping({"/driver"})
public class DriverController extends BaseController {
    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private DriverService driverService;
    @Autowired
    private LicenseService licenseService;

    public DriverController() {
    }

    @GetMapping({"/drivers"})
    @ResponseBody
    public DataGridView list(Driver driver, Integer deptid) {
        Dept dept = new Dept();
        dept.setDeptid(deptid);
        driver.setDept(dept);
        DataGridView dataGridView = this.driverService.findAllDriver(driver);
        return dataGridView;
    }
    @RequestMapping("/addDriver")
    @ResponseBody
    public ResultDto  addDriver(Driver driver){
        if (driver.getStatus()==null){
            driver.setStatus("0");
        }
        ResultDto resultDto=new ResultDto();
        try {
            driverService.addDriver(driver);
            resultDto.setCode(200);
            resultDto.setMsg("增加成功");
            resultDto.setData(driver);
        } catch (Exception e) {
            resultDto.setCode(-200);
            resultDto.setMsg("增加失败");
            e.printStackTrace();
        }
        return resultDto;
    }
    @ResponseBody
    @RequestMapping("/license")
    public List<License> licenses(){
        return licenseService.findAllLicense();
    }

    @ResponseBody
    @RequestMapping("/noCarDriver")
    public List<Driver>  noCarDriver(){
        return  driverService.noCar();
    }

    @ResponseBody
    @RequestMapping("/editDriver")
    public ResultDto editDriver(Driver driver){
        logger.info(driver);
        ResultDto resultDto=new ResultDto();
        try {
            driverService.editDriver(driver);
            resultDto.setCode(200);
            resultDto.setMsg("修改成功");
            resultDto.setData(driver);
        } catch (Exception e) {
            resultDto.setCode(-200);
            resultDto.setMsg("修改失败");
            e.printStackTrace();
        }

        return resultDto;
    }
    //批量删除
    @ResponseBody
    @RequestMapping("/deleteBatch")
    public ResultDto deleteDriverBatch(@RequestParam("driverids[]") Integer[] driverids){
        ResultDto resultDto=new ResultDto();
        try {
            driverService.deleteBatch(driverids);
            resultDto.setMsg("删除成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("删除失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }
        return resultDto;
    }

    //单个删除
    @ResponseBody
    @RequestMapping("/delete")
    public ResultDto deleteDriver(Integer driverid){
        ResultDto resultDto=new ResultDto();
        try {
            driverService.updateStatus(driverid);
            resultDto.setMsg("删除成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("删除失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }
        return resultDto;
    }
}
